package com.huluobo.a2202mvvm_im_douyindemo.adapter;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by LC on 2024/4/26.
 */
public class ConversationAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {
    public ConversationAdapter(@Nullable List<EMConversation> data) {
        super(R.layout.item_conversation, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, EMConversation emConversation) {
        //会话列表的名字
        baseViewHolder.setText(R.id.item_conversation_name, emConversation.conversationId());

        //最后一条消息
        switch (emConversation.getLastMessage().getType()) {
            case TXT:
                //文本消息
                EMTextMessageBody body = (EMTextMessageBody) emConversation.getLastMessage().getBody();
                baseViewHolder.setText(R.id.item_conversation_last_message, body.getMessage());
                break;
            case IMAGE:
                baseViewHolder.setText(R.id.item_conversation_last_message, "[图片]");
                break;
            case FILE:
                baseViewHolder.setText(R.id.item_conversation_last_message, "[文件]");
                break;
            default:
                baseViewHolder.setText(R.id.item_conversation_last_message, "[其他消息]");
                break;
        }

        //时间戳
        String timestampString = DateUtils.getTimestampString(new Date(emConversation.getLastMessage().getMsgTime()));
        baseViewHolder.setText(R.id.item_conversation_timestamp, timestampString);

        //未读消息数量
        if (emConversation.getUnreadMsgCount() > 0) {
            baseViewHolder.getView(R.id.item_conversation_unreadCount).setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.item_conversation_unreadCount, emConversation.getUnreadMsgCount() + "");
        } else {
            baseViewHolder.getView(R.id.item_conversation_unreadCount).setVisibility(View.GONE);
        }
    }
}
